package com.nbx.app.neheport.render;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.nbx.app.neheport.R;
import com.nbx.library.glutes.shape;
import com.nbx.library.glutes.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.view.MotionEvent;

public class Render19 extends RenderBase {

	private int	mTextureID[];
	static final int MAX_PARTICLES=1000;
	
	boolean	rainbow=true;
	boolean	sp;
	boolean	rp;
	float	slowdown=2.0f;
	float	xspeed;
	float	yspeed;
	float	zoom=-40.0f;
	int	loop;
	int	col;
	int	delay;
	
    private float box[] = {
            // FRONT
            -0.5f, -0.5f,  0.5f,
             0.5f, -0.5f,  0.5f,
            -0.5f,  0.5f,  0.5f,
             0.5f,  0.5f,  0.5f,
    };
    
    private float texCoords[] = {
            // FRONT
             0.0f, 0.0f,
             1.0f, 0.0f,
             0.0f, 1.0f,
             1.0f, 1.0f,
    };
    
    private FloatBuffer mCubeVetexBuffer;
    private FloatBuffer mCubeTexBuffer;
	
	class particles
	{
		boolean	active;
		float	life;
		float	fade;
		float	r;
		float	g;
		float	b;
		float	x;
		float	y;
		float	z;
		float	xi;	
		float	yi;	
		float	zi;

		float	xg;
		float	yg;	
		float	zg;	
	};

	particles particle[] = new particles[MAX_PARTICLES];

	static float colors[][]=
	{
		{1.0f,0.5f,0.5f},{1.0f,0.75f,0.5f},{1.0f,1.0f,0.5f},{0.75f,1.0f,0.5f},
		{0.5f,1.0f,0.5f},{0.5f,1.0f,0.75f},{0.5f,1.0f,1.0f},{0.5f,0.75f,1.0f},
		{0.5f,0.5f,1.0f},{0.75f,0.5f,1.0f},{1.0f,0.5f,1.0f},{1.0f,0.5f,0.75f}
	};  

    public Render19(Context context) {
        super(context);
    }

    public void onDrawFrame(GL10 gl) {

    	gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    	gl.glLoadIdentity();
    	
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[0]);
        
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mCubeVetexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mCubeTexBuffer);        
        
    	for (loop=0;loop<MAX_PARTICLES;loop++)
    	{
    		if (particle[loop].active)
    		{
    			float x=particle[loop].x;
    			float y=particle[loop].y;
    			float z=particle[loop].z+zoom;

    			gl.glColor4f(particle[loop].r,particle[loop].g,particle[loop].b,particle[loop].life);
    			
    			mCubeVetexBuffer.put(0, x-0.5f);
    			mCubeVetexBuffer.put(1, y-0.5f);
    			mCubeVetexBuffer.put(2, z);
    			
    			mCubeVetexBuffer.put(3, x+0.5f);
    			mCubeVetexBuffer.put(4, y-0.5f);
    			mCubeVetexBuffer.put(5, z);
    			
    			mCubeVetexBuffer.put(6, x-0.5f);
    			mCubeVetexBuffer.put(7, y+0.5f);
    			mCubeVetexBuffer.put(8, z);
    			
    			mCubeVetexBuffer.put(9,  x+0.5f);
    			mCubeVetexBuffer.put(10, y+0.5f);
    			mCubeVetexBuffer.put(11, z);
    			
    	        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    	           			
    			particle[loop].x+=particle[loop].xi/(slowdown*1000);
    			particle[loop].y+=particle[loop].yi/(slowdown*1000);
    			particle[loop].z+=particle[loop].zi/(slowdown*1000);
    			particle[loop].xi+=particle[loop].xg;
    			particle[loop].yi+=particle[loop].yg;
    			particle[loop].zi+=particle[loop].zg;
    			particle[loop].life-=particle[loop].fade;
    			if (particle[loop].life<0.0f)
    			{
    				particle[loop].life=1.0f;
    				particle[loop].fade=(float)(Math.random()*0.1f+0.003f);
    				particle[loop].x=0.0f;
    				particle[loop].y=0.0f;					
    				particle[loop].z=0.0f;				
    				particle[loop].xi=xspeed + (float)(Math.random()*60.0f-32.0f);
    				particle[loop].yi=yspeed + (float)(Math.random()*60.0f-30.0f);	
    				particle[loop].zi=(float)(Math.random()*60.0f-30.0f);	
    				particle[loop].r=colors[col][0];
    				particle[loop].g=colors[col][1];			
    				particle[loop].b=colors[col][2];			
    			}
    			
    			/*
    			if (keys[VK_NUMPAD8] && (particle[loop].yg<1.5f)) particle[loop].yg+=0.01f;
    			
    			if (keys[VK_NUMPAD2] && (particle[loop].yg>-1.5f)) particle[loop].yg-=0.01f;
    			
    			if (keys[VK_NUMPAD6] && (particle[loop].xg<1.5f)) particle[loop].xg+=0.01f;
    			
    			if (keys[VK_NUMPAD4] && (particle[loop].xg>-1.5f)) particle[loop].xg-=0.01f;
    			
    			if (keys[VK_TAB])						
    			{
    				particle[loop].x=0.0f;					
    				particle[loop].y=0.0f;					
    				particle[loop].z=0.0f;					
    				particle[loop].xi=float((rand()%50)-26.0f)*10.0f;
    				particle[loop].yi=float((rand()%50)-25.0f)*10.0f;	
    				particle[loop].zi=float((rand()%50)-25.0f)*10.0f;	
    			}*/
    		}
        }
    	
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		if (height == 0) {
			height = 1;
		}

		gl.glViewport(0, 0, width, height);

		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();

		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
				100.0f);

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    	loadBitmapTex(gl);
        mCubeVetexBuffer = util.createFloatBuffer(box);
        mCubeTexBuffer = util.createFloatBuffer(texCoords);
    	
    	gl.glEnable(GL10.GL_TEXTURE_2D);
    	gl.glShadeModel(GL10.GL_SMOOTH);
    	gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    	gl.glClearDepthf(1.0f);
    	gl.glEnable(GL10.GL_DEPTH_TEST);
   
    	gl.glDisable(GL10.GL_DEPTH_TEST);
    	
		for (loop = 0; loop < MAX_PARTICLES; loop++) {
			particle[loop] = new particles();
			particle[loop].active = true;
			particle[loop].life = 1.0f;
			particle[loop].fade = (float) (Math.random() * 0.1f + 0.003f);
			particle[loop].r = colors[loop * (12 / MAX_PARTICLES)][0];
			particle[loop].g = colors[loop * (12 / MAX_PARTICLES)][1];
			particle[loop].b = colors[loop * (12 / MAX_PARTICLES)][2];
			particle[loop].xi = (float) (((Math.random() * 50) - 26.0f) * 10.0f);
			particle[loop].yi = (float) (((Math.random() * 50) - 25.0f) * 10.0f);
			particle[loop].zi = (float) (((Math.random() * 50) - 25.0f) * 10.0f);
			particle[loop].xg = 0.0f;
			particle[loop].yg = -0.8f;
			particle[loop].zg = 0.0f;
		}
  
    	gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    	gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
    	gl.glEnable(GL10.GL_BLEND);	
    }

    private void loadBitmapTex(GL10 gl){
        mTextureID = new int[1];
        gl.glGenTextures(1, mTextureID, 0);

        Bitmap bmp = util.getTextureFromBitmapResource(mContext, R.drawable.particle);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[0]);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
        bmp.recycle();

        return;
    }

}
